<?php

namespace App\Imports\BatchQuality;

use App\Models\ProductBatchQualityModel;
use App\Models\ProductsBatchModel;
use Illuminate\Database\Eloquent\Model;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithStartRow;

class ImportQualityResult implements ToModel,WithStartRow
{
    protected $batch_id;

    public function __construct($batch_id)
    {
        $this->batch_id = $batch_id;
    }

    /**
     * @param array $row
     *
     * @return Model|null
     */
    public function model(array $row)
    {

        /** @var ProductBatchQualityModel $quality */
        $quality = ProductBatchQualityModel::create([
            'no' => $row[0],
            'product_name' => $row[1],
            'test_name' => $row[2],
            'test_result1' => $row[3],
            'test_result' => $row[4],
            'start_test_time' => $row[5],
            'test_compony' => $row[6],
            'test_user' => $row[7],
            'test_time' => $row[8],
            'channel_id' => $row[9],
            'test_device' => $row[10],
        ]);

        $batch = ProductsBatchModel::find($this->batch_id);
        $batch->quality_result_id = $quality->getKey();
        $batch->save();
        return $quality;
    }


    /**
     * 从第几行开始处理数据 就是不处理标题
     * @return int
     */
    public function startRow(): int
    {
        return 2;
    }
}
